//
//  LoginViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/2.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "AngelLeftMenuViewController.h"
#import "CarOwnerLeftMenuViewController.h"
#import "RegisterViewController.h"
#import "ResetPassWordViewController.h"
#import "OwnerHomeViewController.h"
#import "Constant.h"
#import "AFNetworking.h"


@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
@property (weak, nonatomic) IBOutlet UITextField *userText;
@property (weak, nonatomic) IBOutlet UITextField *psdText;


//登录方法
- (IBAction)loginBtn:(UIButton *)sender;
//密码重置方法
- (IBAction)ResetPsd:(UIButton *)sender;
//记住密码状态改变方法
- (IBAction)RememberPsd:(id)sender;
//注册用户
- (IBAction)registerUser:(id)sender;


//额外添加
@property (nonatomic, assign) int a;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.logoImageView.image=[UIImage imageNamed:@"logo"];
   // self.logoImageView.backgroundColor=[UIColor clearColor];
    
    //设置文本框样式
    UIImageView *user = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user.png"]];
    user.frame = CGRectMake(0, 0, self.userText.frame.size.height*0.5, self.userText.frame.size.height*0.5);
    
    self.userText.leftView = user;
    self.userText.leftViewMode = UITextFieldViewModeAlways;
    self.userText.clearButtonMode = UITextFieldViewModeAlways;//输入时出现叉号，用于一次性删除文本框里面的内容
    
    UIImageView *psd = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"password.png"]];
    psd.frame = CGRectMake(0, 0, self.userText.frame.size.height*0.5, self.userText.frame.size.height*0.5);
    self.psdText.leftView = psd;
    self.psdText.leftViewMode = UITextFieldViewModeAlways;
    self.psdText.clearButtonMode = UITextFieldViewModeAlways;//输入时出现叉号，用于一次性删除文本框里面的内容
    
    //设置代理,为下面textField的键盘收回打基础
    self.userText.delegate = self;
    self.psdText.delegate = self;
    
    self.a = 0;
}
-(void)viewWillAppear:(BOOL)animated{

}
//return键返回响应
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//登录方法
- (IBAction)loginBtn:(UIButton *)sender {
    //网络请求
    AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
    request.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"userName"] = @"15515557023";
    dic[@"password"] = @"123456";
    dic[@"clientID"] = @"";

    [request POST:[NSString stringWithFormat:@"%@%@",url1,@"Login"] parameters:dic success:^(AFHTTPRequestOperation * operate, id responObj) {
        NSDictionary *dic = (NSDictionary *)responObj;
        if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"-1"]) {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码错误！" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        }else
        {
            NSDictionary *dic = (NSDictionary *)responObj;
            NSDictionary *data = dic[@"Data"];
            NSString *token = data[@"token"];
            NSString *role = data[@"role"];
            NSLog(@"role:%@",data[@"role"]);
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            tempAppDelegate.token = token;//token为全app可用
            NSLog(@"TOKEN :%@",tempAppDelegate.token);
            if ([role isEqualToString:@"Company"]) {//区域公司
                
            }else if ([role isEqualToString:@"CType"]){//C类联盟商
                
            }else if ([role isEqualToString:@"InstallPoint"]){//安装点
                
            }else if ([role isEqualToString:@"User"]){//蓝天使者
                OwnerHomeViewController *mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"OwnerHomeViewController"];
                tempAppDelegate.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
                CarOwnerLeftMenuViewController *leftVC = [[CarOwnerLeftMenuViewController alloc] init];
                tempAppDelegate.LeftSlideVC = [[LeftSlideViewController alloc] initWithLeftView:leftVC andMainView:tempAppDelegate.mainNavigationController];
                
                [self presentViewController:tempAppDelegate.LeftSlideVC animated:YES completion:nil];
                
            }else  {//车主用户
                OwnerHomeViewController *mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"OwnerHomeViewController"];
                tempAppDelegate.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
                
                CarOwnerLeftMenuViewController *leftVC = [[CarOwnerLeftMenuViewController alloc] init];
                
                tempAppDelegate.LeftSlideVC = [[LeftSlideViewController alloc] initWithLeftView:leftVC andMainView:tempAppDelegate.mainNavigationController];
                
                [self presentViewController:tempAppDelegate.LeftSlideVC animated:YES completion:nil];
            }
        }
    } failure:^(AFHTTPRequestOperation * operate, NSError * error) {
        NSLog(@"%@",@"系统请求错误！");
    }];
    
    
    
    
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //蓝天使者登录
//    if (self.a == 0) {
//        HomeViewController *mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
//        tempAppDelegate.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
//        AngelLeftMenuViewController *leftVC = [[AngelLeftMenuViewController alloc] init];
//        tempAppDelegate.LeftSlideVC = [[LeftSlideViewController alloc] initWithLeftView:leftVC andMainView:tempAppDelegate.mainNavigationController];
//        [self presentViewController:tempAppDelegate.LeftSlideVC animated:YES completion:nil];
//    }else
    //{
        //车主用户登录
        OwnerHomeViewController *mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"OwnerHomeViewController"];
        tempAppDelegate.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
        CarOwnerLeftMenuViewController *leftVC = [[CarOwnerLeftMenuViewController alloc] init];
        
        tempAppDelegate.LeftSlideVC = [[LeftSlideViewController alloc] initWithLeftView:leftVC andMainView:tempAppDelegate.mainNavigationController];
        
       // [self presentViewController:tempAppDelegate.LeftSlideVC animated:YES completion:nil];
    //}
}


-(void)viewDidAppear:(BOOL)animated{
    
}


//密码重置方法
- (IBAction)ResetPsd:(UIButton *)sender {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ResetPassWordViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"ResetPassWordViewController"];
    [self presentViewController:vc animated:YES completion:nil];
}

//记住密码状态改变方法
- (IBAction)RememberPsd:(UIButton *)sender {
    if (self.a == 0) {
        [sender setBackgroundImage:[UIImage imageNamed:@"icon_user"] forState:UIControlStateNormal];
        self.a = 1;
    }else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"psd"] forState:UIControlStateNormal];
        self.a = 0;
    }
}

- (IBAction)registerUser:(id)sender {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    RegisterViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"RegisterViewController"];
    [self presentViewController:vc animated:YES completion:nil];
}
@end
